package com.ruoyi.system.controller.config;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import jakarta.websocket.CloseReason;
import jakarta.websocket.EndpointConfig;
import jakarta.websocket.Session;
import jakarta.websocket.server.ServerEndpoint;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.net.GuacamoleSocket;
import org.apache.guacamole.net.GuacamoleTunnel;
import org.apache.guacamole.net.InetGuacamoleSocket;
import org.apache.guacamole.net.SimpleGuacamoleTunnel;
import org.apache.guacamole.protocol.ConfiguredGuacamoleSocket;
import org.apache.guacamole.protocol.GuacamoleClientInformation;
import org.apache.guacamole.protocol.GuacamoleConfiguration;
import org.apache.guacamole.websocket.GuacamoleWebSocketTunnelEndpoint;
import org.springframework.stereotype.Component;


import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author pancm
 * @Title: WebSocketTunnel
 * @Description:
 * @Version:1.0.0
 * @Since:jdk1.8
 * @Date 2021/8/2
 **/
@ServerEndpoint(value = "/bl/webSocket", subprotocols = "guacamole")
@Component
public class WebSocketTunnel extends GuacamoleWebSocketTunnelEndpoint {

    @Override
    protected GuacamoleTunnel createTunnel(Session session, EndpointConfig endpointConfig) throws GuacamoleException {
        System.out.println("sessionMap:" + session.getRequestParameterMap());
        // 获取url的值
        int height = Integer.parseInt(session.getRequestParameterMap().get("height").get(0).split("\\.")[0]);
        int width = Integer.parseInt(session.getRequestParameterMap().get("width").get(0).split("\\.")[0]);
        GuacamoleClientInformation information = new GuacamoleClientInformation();
        information.setOptimalScreenHeight(height);
        information.setOptimalScreenWidth(width);
//        information.setOptimalResolution(96);
//        information.setName("lyq");
        information.setTimezone("Asia/Shanghai");


        List<String> audioMimetypes = information.getAudioMimetypes();
        audioMimetypes.add("audio/L8");
        audioMimetypes.add("audio/L16");

        List<String> imageMimetypes = information.getImageMimetypes();
        imageMimetypes.add("image/jpeg");
        imageMimetypes.add("image/png");
        imageMimetypes.add("image/webp");


        //guacamole server地址 r端口
        String hostname = "10.10.10.100";
        int port = 4822;
        GuacamoleConfiguration configuration = new GuacamoleConfiguration();

        configuration.setProtocol("rdp");
        // 远程windows服务的地址
        configuration.setParameter("hostname", "10.10.10.204");
        configuration.setParameter("port", "3389");
        configuration.setParameter("username", "administrator");
        configuration.setParameter("password", "Lyq521..");
        configuration.setParameter("ignore-cert", "true");

        configuration.setParameter("enable-drive", "true");
        configuration.setParameter("create-drive-path", "true");
        configuration.setParameter("drive-path", "/home/file");
        configuration.setParameter("client-name", "tytytt");


//        configuration.setProtocol("vnc");
//        // 远程windows服务的地址
//        configuration.setParameter("hostname", "192.168.80.60");
//        configuration.setParameter("port", "5900");
//        configuration.setParameter("username", "IndSvr");
//        configuration.setParameter("password", "981008");
////        configuration.setParameter("ignore-cert", "true");

//
//        configuration.setProtocol("ssh");
//        // 远程windows服务的地址
//        configuration.setParameter("hostname", "10.10.10.4");
//        configuration.setParameter("port", "22");
//        configuration.setParameter("username", "root");
//        configuration.setParameter("password", "Lyq521..");
//        configuration.setParameter("ignore-cert", "true");


        String fileName = getNowTime() + ".guac";//文件名
        String outputFilePath = "/home/guacamole";
        //添加会话录制--录屏
        configuration.setParameter("recording-path", outputFilePath);
        configuration.setParameter("create-recording-path", "true");
        configuration.setParameter("recording-name", fileName);


        configuration.setParameter("enable-wallpaper", "true");//壁纸
        configuration.setParameter("enable-theming", "true");//主题
        configuration.setParameter("enable-full-window-drag", "true");//
        configuration.setParameter("enable-font-smoothing", "true");//
        configuration.setParameter("enable-menu-animations", "true");//
        configuration.setParameter("enable-desktop-composition", "true");//

        GuacamoleSocket socket = new ConfiguredGuacamoleSocket(
                new InetGuacamoleSocket(hostname, port),
                configuration,
                information
        );


        GuacamoleTunnel tunnel = new SimpleGuacamoleTunnel(socket);

        return tunnel;
    }

    private void optClose(Session session) {
        // 判断当前连接是否还在线
        if (session.isOpen()) {
            try {
                // 关闭连接
                CloseReason closeReason = new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "进行关闭！");
                session.close(closeReason);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private String getNowTime() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
    }
}
